<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';
require_once ROOT_SERVICES_HELPER . DS . 'curl.php';

// viettel_post
class shipping_provider_13 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        try
        {   
            $tracking_number = $this->_shipment ['tracking_number'];
            $str_link = $this->_shipment ['url'] !== '' ? $this->_shipment ['url'] : "http://viettelpost.com.vn/Default.aspx?tabid=208&id=";
            $str_link .= $tracking_number;

            $html =  str_get_html(curl::instance()->get($str_link));
            $status_list = array();
            if ($html) {
                foreach ($html->find('table[id=dnn_ctr759_Tracuu_HanhTrinh_Find_grdDS] tbody tr') as $tr) {
                    $row = array();
                    foreach ($tr->find('td') as $td) {
                        $row [] = $td->plaintext;
                    }
                    if (isset($row [0])) {
                        array_shift($row);
                        $time_a = array_shift($row);
                        $matches = array();
                        preg_match("/([0-9]{2})[\:]([0-9]{2}) ([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $time_a, $matches);
                        list ( $date, $H, $i, $j, $m, $Y ) = $matches;

                        $status_list [] = array(
                            'time' => "{$Y}-{$m}-{$j} {$H}:{$i}:00",
                            'status' => "({$this->_shipment ['shipment_provider_name']}) " . html_entity_decode($row [0], ENT_QUOTES, 'UTF-8')
                        );
                    }
                }
            }
            return array('sortable' => TRUE, 'status_list' => $status_list);
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
